package com.glsc.ngateway.opmanage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.TOaFlowMetaTable;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.opmanage.service.WorkflowService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/meta")
public class WorkFlowController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private WorkflowService workflowService;

    @Autowired
    public void setWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    @PostMapping("/createFlowMetaData")
    @ApiOperation("新增流程字段")
    public PlatformResponse<Void> createFlowMetaData(@RequestBody Object param) {

//        System.out.println(param.getClass());

        Map<String, Object> map = (Map) JSON.toJSON(param);

        for (int i = 0; i < map.size(); i++) {
            JSONObject obj = JSON.parseObject(map.get(String.valueOf(i)).toString());
            TOaFlowMetaTable fmd = JSONObject.toJavaObject(obj, TOaFlowMetaTable.class);
            workflowService.add(fmd);
        }
        return PlatformResponse.successMsg("新增字段成功");
    }

    @GetMapping("/getFlowMetaData")
    @ApiOperation("查询流程字段分页")
    public PlatformResponse<Page<TOaFlowMetaTable>> getFlowMetaData(
            @ApiParam(value = "流程ID") @RequestParam("workFlowId") String workFlowId,
            @ApiParam(value = "流程字段名") @RequestParam(value = "interfaceFieldName", defaultValue = "", required = false) String interfaceFieldName,
            @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = PageConstant.PageSize;
        }

        Page<TOaFlowMetaTable> page = workflowService.getFlowMetaData(workFlowId, interfaceFieldName, pageNo, pageSize);
        return PlatformResponse.successData(page);
    }

    @GetMapping("/listAllFlow")
    @ApiOperation("查询全部流程分页")
    public PlatformResponse<Page<TOaFlowMetaTable>> listAllFlow(
            @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {


        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = PageConstant.PageSize;
        }

        Page<TOaFlowMetaTable> page = workflowService.getAllFlow(pageNo, pageSize);
        return PlatformResponse.successData(page);
    }

    @GetMapping("/deleteFlowMetaData")
    @ApiOperation("删除流程字段")
    public PlatformResponse<Void> deleteFlowMetaData(
            @ApiParam(value = "流程ID") @RequestParam("workFlowId") String workFlowId,
            @ApiParam(value = "流程字段名") @RequestParam(value = "interfaceFieldName", defaultValue = "", required = false) String interfaceFieldName) {

        if (workflowService.deleteFlowMetaData(workFlowId, interfaceFieldName) > 0) {
            return PlatformResponse.successMsg("删除成功");
        } else {
            return PlatformResponse.failedMsg("未找到相关字段");
        }
    }

    @PostMapping("/updateFlowMetaData")
    @ApiOperation("更新流程字段")
    public PlatformResponse<Void> updateFlowMetaData(@RequestBody Object param) {

        workflowService.update(param);

//        Map<String, Object> map = (Map) JSON.toJSON(param);
//
//        for(int i = 0; i < map.size(); i++) {
//            JSONObject obj = JSON.parseObject(map.get(String.valueOf(i)).toString());
//            FlowMetaData fmd = JSONObject.toJavaObject(obj,FlowMetaData.class);
//            workflowService.update(fmd);
//        }
        return PlatformResponse.successMsg("更新字段成功");
    }


//    @ResponseBody
//    @RequestMapping("/importFile")
//    public JSONObject importFile(HttpServletRequest request) throws IOException {
//        JSONObject json = new JSONObject();
//
//        CommonsMultipartResolver multipartResolver =
//                new CommonsMultipartResolver(request.getSession().getServletContext());
//        //检查form中是否有enctype="multipart/form-data"
//
//        if (multipartResolver.isMultipart(request)) {
//            //将request变成多request
//            MultipartHttpServletRequest multiRequest =
//                    (MultipartHttpServletRequest) request;
//
//
//            MultipartFile multipartFile = multiRequest.getFile("file");
//
//            XSSFWorkbook wb = null;
//            try {
//                wb =  new XSSFWorkbook(multipartFile.getInputStream());
//                // logger.debug(wb.getNumberOfSheets());
//                XSSFSheet sheet = wb.getSheet("Sheet1");
//                List<Map<String,String>> dataList = workflowService.excelDataToMap(sheet);
//
//                json.put("data", dataList);
//                json.put("code", "000000");
//                json.put("msg", "success");
//
//            } catch(Exception e){
//                logger.error(e.getMessage());
//                json.put("code", "333333");
//                json.put("msg", "上传文件失败");
//            }
//        }
//        return json;
//    }

}
